//Redis缓存中间件
// middleware/cacheHandler.js
module.exports = async (req, res, next) => {
    const { lat, lon, hours = 24 } = req.query;
    const cacheKey = `air:${lat}:${lon}:${hours}`;
  
    try {
      const cachedData = await redis.get(cacheKey);
      if (cachedData) {
        res.set('X-Cache', 'HIT');
        return res.json(JSON.parse(cachedData));
      }
      
      res.locals.cacheKey = cacheKey;
      next();
    } catch (error) {
      console.error('缓存错误:', error);
      next();
    }
  };